package com.zheng.cms.rocketmq.consumer;


import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.zheng.cms.rocketmq.listener.WithdrawMessageListener;
import com.zheng.cms.rocketmq.rocketmq.WithdrawMsgProducer;
import com.zheng.cms.rocketmq.utils.CommonConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Properties;

public class WithdrawMsgConsumer {
    private final Logger logger = LoggerFactory.getLogger(WithdrawMsgProducer.class);

    private Consumer consumer;

    private String consumerId;
    private String accessKey;
    private String secretKey;
    private String onsAddr;
    private String topic;
    private String tag;

    @Autowired
    private WithdrawMessageListener withdrawMessageListener;

    public void init() {

        Properties consumerProperties = new Properties();
        consumerProperties.setProperty(PropertyKeyConst.ConsumerId, CommonConstants.MQ_CID);
        consumerProperties.setProperty(PropertyKeyConst.AccessKey, CommonConstants.MQ_AccessKey);
        consumerProperties.setProperty(PropertyKeyConst.SecretKey, CommonConstants.MQ_SecretKey);
        consumerProperties.setProperty(PropertyKeyConst.ONSAddr, CommonConstants.MQ_ONSAddr);
        consumerProperties.put(PropertyKeyConst.ConsumeThreadNums,60);

        consumer = ONSFactory.createConsumer(consumerProperties);
        consumer.subscribe(CommonConstants.MQ_AUTO_TOPIC, CommonConstants.MQ_AUTO_TAG, withdrawMessageListener);
        consumer.start();

    }

    public void destroy() {
        consumer.shutdown();
    }

    public String getConsumerId() {
        return consumerId;
    }

    public void setConsumerId(String consumerId) {
        this.consumerId = consumerId;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getOnsAddr() {
        return onsAddr;
    }

    public void setOnsAddr(String onsAddr) {
        this.onsAddr = onsAddr;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

}
